package com.example.astronautcontact2206044090cxt1104;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class ContactDAL {
    private ContactSQLiteOpenHelper contactSQLiteOpenHelper;
    private SQLiteDatabase sqLiteDatabase;
    private Context context;

    public ContactDAL(Context context) {
            this.context = context;
            }
    public void openDatabase() {
         contactSQLiteOpenHelper = new
                ContactSQLiteOpenHelper(context);
         try {
             sqLiteDatabase = contactSQLiteOpenHelper.getWritableDatabase();
             } catch (Exception e) {
             sqLiteDatabase = contactSQLiteOpenHelper.getReadableDatabase();
             }
         }

    public long delete(int id){
        long result =-1;
        try {
            openDatabase();
            result = sqLiteDatabase.delete("contacts","id=?",new String[]{String.valueOf(id)});
            closeDatabase();
        }catch (Exception e) {

        }
        return result;
    }

    public void closeDatabase() {
          if (sqLiteDatabase != null) {
              sqLiteDatabase.close();
              sqLiteDatabase = null;
             }
         if (contactSQLiteOpenHelper != null) {
             contactSQLiteOpenHelper.close();
             contactSQLiteOpenHelper = null;
             }
         }

    public long insertContact(AstronautContact contact)
    {
        long result = -1;
        ContentValues values = new ContentValues();
        values.put("name", contact.getName());
        values.put("email", contact.getEmail());
        values.put("phone", contact.getPhone());
        values.put("organization", contact.getOrganization());
        values.put("notes", contact.getNotes());
        try {
            openDatabase();
            result = sqLiteDatabase.insert("contacts",null,values);
            closeDatabase();
        }catch (Exception e){

        }
        return result;
    }
    public List<AstronautContact> queryAll() {
        List<AstronautContact> contacts = new ArrayList<>();
        try {
            openDatabase();
            Cursor cursor =sqLiteDatabase.query("contacts", null, null,
                    null,null, null, null, null);
            int idColumnIndex = cursor.getColumnIndex("id");
            int nameColumnIndex = cursor.getColumnIndex("name");
            int phoneColumnIndex = cursor.getColumnIndex("phone");
            int emailColumnIndex = cursor.getColumnIndex("email");
            int organizationColumnIndex = cursor.getColumnIndex("organization");
            int notesColumnIndex = cursor.getColumnIndex("notes");
            if (cursor.moveToFirst()) {
                do {
                    int id = cursor.getInt(idColumnIndex);
                    String name = cursor.getString(nameColumnIndex);
                    String phone = cursor.getString(phoneColumnIndex);
                    String email = cursor.getString(emailColumnIndex);
                    String organization = cursor.getString(organizationColumnIndex);
                    String notes = cursor.getString(notesColumnIndex);
                    contacts.add(new AstronautContact(id,name, phone,email, organization, notes));
                } while (cursor.moveToNext());
            }
            cursor.close();
            closeDatabase();
        } catch (Exception e) {
        }
        return contacts;
    }
    public AstronautContact queryById(int id){
        AstronautContact contact =null;
        try{
            openDatabase();
            Cursor cursor =sqLiteDatabase.query("contacts",null,"id=?",
                    new String[]{String.valueOf(id)},null,null,null,null);
            if(cursor.moveToFirst()){
                int nameColumnIndex = cursor.getColumnIndex("name");
                int phoneColumnIndex = cursor.getColumnIndex("phone")
                        ;
                int emailColumnIndex = cursor.getColumnIndex("email")
                        ;
                int organizationColumnIndex = cursor.getColumnIndex("organization");
                int notesColumnIndex = cursor.getColumnIndex("notes")
                        ;
                String name = cursor.getString(nameColumnIndex);
                String phone = cursor.getString(phoneColumnIndex);
                String email = cursor.getString(emailColumnIndex);
                String organization = cursor.getString(organizationColumnIndex);
                String notes = cursor.getString(notesColumnIndex);
                contact = new AstronautContact(id,name, phone, email,
                        organization, notes);
            }
            closeDatabase();

        }catch (Exception e){

        }
        return contact;
    }
    public long update(AstronautContact contact){
        long result;
        ContentValues values = new ContentValues();
        values.put("name", contact.getName());
        values.put("phone", contact.getPhone());
        values.put("email", contact.getEmail());
        values.put("organization", contact.getOrganization());
        values.put("notes", contact.getNotes());
        try {
            openDatabase();
            result = sqLiteDatabase.update("contacts", values, "id=?",
                    new String[]{String.valueOf(contact.getId())});
            closeDatabase();
        } catch (Exception e) {
            result = -1;
        }
        return result;
    }
}
